package edu.hunau.cxb19.oop.class14.hk;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.temporal.ChronoField;
import java.time.temporal.TemporalField;
import java.util.Calendar;
import java.util.Date;

import org.junit.Test;

/**
 *
 * @author hb
 *
 *         2020-03-13 14:41:19
 */
public class DateTest {

	@Test
	public void test() throws Exception {
		Date date = special("2020-01-01", 3);
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		System.out.println(dateFormat.format(date));

		LocalDate newSpecial = newSpecial("2020-01-01", 3);
		System.out.println(newSpecial);
	}

	/**
	 * 获得商品的促销时间
	 * 
	 * @param createDate
	 * @param months
	 * @return 过期前的两周的周五
	 */
	public Date special(String createDate, int months) throws Exception {
		// 1. 将字符串转成日期类型
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		Date date = dateFormat.parse(createDate);
		// 2. 获得商品的过期时间
		// 2.1 将date类型转成Calendar类型
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		// 2.2 获得过期的时间
		calendar.add(Calendar.MONTH, months);
		// 2.3 获得过期的前两周
		calendar.add(Calendar.WEEK_OF_YEAR, -2);
		// 2.4 指定时间为周五
		calendar.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
		return calendar.getTime();
	}

	/**
	 * 获得商品的促销时间
	 * 
	 * @param createDate
	 * @param months
	 * @return 过期前的两周的周五
	 */
	public LocalDate newSpecial(String createDate, int months) {
		LocalDate date = LocalDate.parse(createDate);// 采用默认的格式转换
		// 获得过期时间
		LocalDate newDate = date.plusMonths(months);
		LocalDate minusWeeks = newDate.minusWeeks(2);
		return minusWeeks.with(ChronoField.DAY_OF_WEEK, 5);
	}

}
